package com.world.generator;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.sql.Types;
import java.util.Collections;

/**
 * 代码快速生成
 *
 * @author 邓驴
 * @date 2023/5/13 14:07
 **/
public class FastGenerator {

    public void fastGenerator(String tableName, String packageName, String url, String username, String password) {

        String parent = "com.world.business." + packageName;

        //桌面路径
        File desktopDir = FileSystemView.getFileSystemView().getHomeDirectory();
        String desktopPath = desktopDir.getAbsolutePath();

        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> builder.author("邓驴")
                        .enableSwagger()
                        .outputDir(desktopPath + "\\world\\business\\src\\main\\java"))
                .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                    int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                    if (typeCode == Types.SMALLINT) {
                        // 自定义类型转换
                        return DbColumnType.INTEGER;
                    } else if (typeCode == Types.TINYINT) {
                        return DbColumnType.INTEGER;
                    } else if (typeCode == Types.DOUBLE) {
                        return DbColumnType.DOUBLE;
                    }
                    return typeRegistry.getColumnType(metaInfo);
                }))
                .packageConfig(builder -> builder.parent(parent)
                        .controller("controller")
                        .entity("domain")
                        .service("service")
                        .mapper("mapper")
                        .pathInfo(Collections.singletonMap(OutputFile.xml, desktopPath + "\\world\\business\\src\\main\\java")))
                .strategyConfig(builder -> builder.addInclude(tableName)
                        .entityBuilder()
                        .enableLombok()
                        .enableChainModel()
                        .mapperBuilder()
                        .enableMapperAnnotation()//开启mapper注解
                        .enableBaseResultMap()//启用xml文件中的BaseResultMap 生成
                        .enableBaseColumnList()//启用xml文件中的BaseColumnList
                        .formatMapperFileName("%sMapper")//格式化Dao类名称
                        .formatXmlFileName("%sMapper")//格式化xml文件名称
                        .serviceBuilder()
                        .formatServiceFileName("I%sService")//格式化 service 接口文件名称
                        .formatServiceImplFileName("%sServiceImpl")//格式化 service 接口文件名称
                        .controllerBuilder()
                        .enableRestStyle()
                        .enableFileOverride())
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }

}

